﻿/* QF classbuilder for Enlib */
using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

#region SYS_User实体类
public class SYS_User
{
	private string m_UserName;
	private string m_Password;
	private DateTime m_LastModifiedTime;
	private string m_LastModifiedByID;
	private DateTime m_LastRefresh;
	private int m_Disabled;
	
	public SYS_User ()
	{
		m_UserName="";
		m_Password="";
		m_LastModifiedTime=DateTime.Now;
		m_LastModifiedByID="";
		m_LastRefresh=DateTime.Now;
		m_Disabled=0;
	}
	
	public string UserName
	{
		get{return m_UserName;}
		set{m_UserName=value;}
	}
	public string Password
	{
		get{return m_Password;}
		set{m_Password=value;}
	}
	public DateTime LastModifiedTime
	{
		get{return m_LastModifiedTime;}
		set{m_LastModifiedTime=value;}
	}
	public string LastModifiedByID
	{
		get{return m_LastModifiedByID;}
		set{m_LastModifiedByID=value;}
	}
	public DateTime LastRefresh
	{
		get{return m_LastRefresh;}
		set{m_LastRefresh=value;}
	}
	public int Disabled
	{
		get{return m_Disabled;}
		set{m_Disabled=value;}
	}
}
#endregion

#region SYS_User管理类
public partial class LogicLayer
{
	public bool AddSYS_User(SYS_User theSYS_User)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "insert into SYS_User (UserName,Password,LastModifiedTime,LastModifiedByID,LastRefresh,Disabled) values (@UserName,@Password,@LastModifiedTime,@LastModifiedByID,@LastRefresh,@Disabled)";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@UserName", DbType.String, theSYS_User.UserName);
			db.AddInParameter(cmd, "@Password", DbType.String, theSYS_User.Password);
			db.AddInParameter(cmd, "@LastModifiedTime", DbType.DateTime, theSYS_User.LastModifiedTime);
			db.AddInParameter(cmd, "@LastModifiedByID", DbType.String, theSYS_User.LastModifiedByID);
			db.AddInParameter(cmd, "@LastRefresh", DbType.DateTime, theSYS_User.LastRefresh);
			db.AddInParameter(cmd, "@Disabled", DbType.Int32, theSYS_User.Disabled);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("AddSYS_User:" + ex.Message );
			return false;
		}
	}
	
	public SYS_User GetSYS_UserFromDataReader(IDataReader dataReader)
	{
		try
		{
			SYS_User result = new SYS_User();
			
			if (dataReader["UserName"] != null && dataReader["UserName"] != System.DBNull.Value )
				result.UserName = (string)dataReader["UserName"];
			if (dataReader["Password"] != null && dataReader["Password"] != System.DBNull.Value )
				result.Password = (string)dataReader["Password"];
			if (dataReader["LastModifiedTime"] != null && dataReader["LastModifiedTime"] != System.DBNull.Value )
				result.LastModifiedTime = (DateTime)dataReader["LastModifiedTime"];
			if (dataReader["LastModifiedByID"] != null && dataReader["LastModifiedByID"] != System.DBNull.Value )
				result.LastModifiedByID = (string)dataReader["LastModifiedByID"];
			if (dataReader["LastRefresh"] != null && dataReader["LastRefresh"] != System.DBNull.Value )
				result.LastRefresh = (DateTime)dataReader["LastRefresh"];
			if (dataReader["Disabled"] != null && dataReader["Disabled"] != System.DBNull.Value )
				result.Disabled = (int)dataReader["Disabled"];
			
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetReaderSYS_User:" + ex.Message);
			return null;
		}
	}
	
	public SYS_User GetSYS_User(string UserName)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * From SYS_User where UserName=@UserName";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@UserName", DbType.String, UserName);
			
			SYS_User result = null;
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
				{
					result = GetSYS_UserFromDataReader( dataReader );
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetSYS_User:" + ex.Message);
			return null;
		}
	}
	
	public bool DelSYS_User(string UserName)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "delete From SYS_User where UserName=@UserName";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@UserName", DbType.String, UserName);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("DelSYS_User:" + ex.Message);
			return false;
		}
	}
	
	public bool UpdateSYS_User(SYS_User theSYS_User)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "update SYS_User set Password=@Password,LastModifiedTime=@LastModifiedTime,LastModifiedByID=@LastModifiedByID,LastRefresh=@LastRefresh,Disabled=@Disabled where UserName=@UserName";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@Password", DbType.String, theSYS_User.Password);
			db.AddInParameter(cmd, "@LastModifiedTime", DbType.DateTime, theSYS_User.LastModifiedTime);
			db.AddInParameter(cmd, "@LastModifiedByID", DbType.String, theSYS_User.LastModifiedByID);
			db.AddInParameter(cmd, "@LastRefresh", DbType.DateTime, theSYS_User.LastRefresh);
			db.AddInParameter(cmd, "@Disabled", DbType.Int32, theSYS_User.Disabled);
			db.AddInParameter(cmd, "@UserName", DbType.String, theSYS_User.UserName);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("UpdateSYS_User:" + ex.Message);
			return false;
		}
	}
	
	public List<SYS_User> ListSYS_User()
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * from SYS_User";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			 List<SYS_User> result = new  List<SYS_User>();
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				while (dataReader.Read())
				{
					SYS_User obj = GetSYS_UserFromDataReader( dataReader );
					if(obj != null)
						result.Add(obj);
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("ListSYS_User:" + ex.Message);
			return null;
		}
	}
	
}
#endregion